//
//  WelcomeViewController.m
//  ModifyMetroDemo
//
//  Created by Marcus on 2020/3/8.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "WelcomeViewController.h"
#import "Masonry.h"
#import "RNSingleViewController.h"
#import "RNParentViewController.h"
#import "RNTimeUtils.h"
#import "RNBusinessInfo.h"
#import "SharePerenceHelper.h"

@interface WelcomeViewController ()

@end

@implementation WelcomeViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  [self initViews];
  [self updateViews];
}

- (void)updateViews{
  RNBusinessInfo* chosenBusinessInfo = [[SharePerenceHelper instance] getReactNativeBusinessInfo];
  if(nil == chosenBusinessInfo){
    self.chosenBusinssLabel.text = @"未选择业务";
  }else{
    self.chosenBusinssLabel.text = [[NSString alloc] initWithFormat:@"已选择：%@", chosenBusinessInfo.displayName];
  }
}

- (void)initViews{
  [self.navigationItem setTitle:@"Welcome"];
  
  UILabel *label = [[UILabel alloc] init];
  label.text=@"Loading...";// 文本内容
  label.font = [UIFont systemFontOfSize:18];// 字体大小
  label.textColor = [UIColor redColor]; // 文本颜色
  [self.view addSubview:label];
  
  [label mas_makeConstraints:^(MASConstraintMaker *make) {
    // make.left.equalTo(self.view);
    // make.right.equalTo(self.view);
    make.width.equalTo(@200);
    make.height.equalTo(@40);
    make.centerX.equalTo(self.view);
    make.centerY.equalTo(self.view).offset(-150);
  }];
  self.chosenBusinssLabel = label;
  
  UIButton *choseButton = [[UIButton alloc] init];
  [choseButton setTitle:@"选择加载的业务" forState:UIControlStateNormal];
  [choseButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  choseButton.backgroundColor = [UIColor grayColor];
  [choseButton setTag: 2]; //
  [self.view addSubview:choseButton];
  [choseButton mas_makeConstraints:^(MASConstraintMaker *make) {
    // make.left.equalTo(self.view);
    // make.right.equalTo(self.view);
    make.width.equalTo(@200);
    make.height.equalTo(@40);
    make.centerX.equalTo(self.view);
    make.centerY.equalTo(self.view).offset(-100);
  }];
  [choseButton addTarget:self action:@selector(btnClickListener:) forControlEvents:UIControlEventTouchUpInside];
  
  UIButton *syncButton = [[UIButton alloc] init];
  [syncButton setTitle:@"Sync Load (同步加载)" forState:UIControlStateNormal];
  [syncButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  syncButton.backgroundColor = [UIColor blueColor];
  [syncButton setTag: 0]; //
  [self.view addSubview:syncButton];
  [syncButton mas_makeConstraints:^(MASConstraintMaker *make) {
    // make.left.equalTo(self.view);
    // make.right.equalTo(self.view);
    make.width.equalTo(@200);
    make.height.equalTo(@40);
    make.center.equalTo(self.view);
  }];
  
  [syncButton addTarget:self action:@selector(btnClickListener:) forControlEvents:UIControlEventTouchUpInside];
  
  
  UIButton *asyncButton = [[UIButton alloc] init];
  [asyncButton setTitle:@"Async Load (异步加载)" forState:UIControlStateNormal];
  [asyncButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  asyncButton.backgroundColor = [UIColor redColor];
  [asyncButton setTag: 1]; //
  [self.view addSubview:asyncButton];
  [asyncButton mas_makeConstraints:^(MASConstraintMaker *make) {
    // make.left.equalTo(self.view);
    // make.right.equalTo(self.view);
    make.width.equalTo(@200);
    make.height.equalTo(@40);
    make.centerX.equalTo(self.view);
    make.centerY.equalTo(self.view).offset(100);
    // make.center.equalTo(self.view);
  }];
  [asyncButton addTarget:self action:@selector(btnClickListener:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)btnClickListener:(UIButton *) button{
  if(button.tag == 0){
    // sync Button
    NSLog(@"Sync Button Clicked.");
    [RNTimeUtils.instance startTimeRecord];
    RNSingleViewController *controller = [[RNSingleViewController alloc] init];
    [self.navigationController pushViewController:controller animated:NO];
  }else if(button.tag == 1){
    // async Button
    NSLog(@"Async Button Clicked.");
    RNParentViewController *controller = [[RNParentViewController alloc] init];
    [self.navigationController pushViewController:controller animated:NO];
  }else if(button.tag == 2){
    // nothing to do.
    //显示弹出框列表选择
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"选择加载的RN业务"
                                                                   message:@""
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    
    
    NSArray* businessArray = [[NSArray alloc] initWithObjects:
                              [[RNBusinessInfo alloc] initWithDisplayName:@"简单业务"
                                                        orgBundleFilePath:@"bundle/index.ios.simple"
                                                       diffBundleFilePath:@"bundle/diff.ios.simple"
                                                        mainComponentName:@"simple"],
                              [[RNBusinessInfo alloc] initWithDisplayName:@"复杂业务"
                                                        orgBundleFilePath:@"bundle/index.ios.complex"
                                                       diffBundleFilePath:@"bundle/diff.ios.complex"
                                                        mainComponentName:@"complex"],
                              
                              nil];
    
    for(RNBusinessInfo* obj in businessArray){
      UIAlertAction* tempAction = [UIAlertAction actionWithTitle:obj.displayName
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * action) {
        //响应事件
        NSLog(@"action = %@", action);
        for (RNBusinessInfo* obj in businessArray) {
          if([obj.displayName isEqualToString:action.title]){
            [SharePerenceHelper.instance saveReactNativeBusinessInfo:obj];
            [self updateViews];
            break;
          }
        }
      }];
      [alert addAction:tempAction];
    }
    [self presentViewController:alert animated:YES completion:nil];
  }
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
